<!-- 编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="750"
    :model-value="modelValue"
    :title="'添加导航链接'"
    @update:modelValue="updateModelValue"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.prevent=""
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="链接名称" prop="name">
            <el-input
              clearable
              v-model="form.name"
              placeholder="请填写链接名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="父级链接" prop="parentId">
            <ele-tree-select
              clearable
              placeholder="请选择父级链接"
              v-model="form.parentId"
              :popper-width="240"
              :tree-props="treeProps"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="显示顺序" prop="orderNum">
            <el-input
              clearable
              v-model="form.orderNum"
              placeholder="请输入显示顺序"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="跳转链接" prop="redirectUrl">
            <el-input
              type="text"
              v-model="form.redirectUrl"
              placeholder="请输入跳转链接"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="显示图标" prop="imageUrl">
            <image-upload
              :limit="1"
              v-model="form.imageUrl"
              :item-style="{ width: '100px', height: '100px', margin: 0 }"
              :button-style="{ width: '100px', height: '100px', margin: 0 }"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!--      <el-form-item label="状态（0正常 1停用）" prop="status">-->
      <!--        <el-input-->
      <!--          clearable-->
      <!--          v-model="form.status"-->
      <!--          placeholder="请输入状态（0正常 1停用）"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="存储图片名称" prop="imageObjectName">-->
      <!--        <el-input-->
      <!--          clearable-->
      <!--          v-model="form.imageObjectName"-->
      <!--          placeholder="请输入存储图片名称"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="图片存储路径" prop="imagePath">-->
      <!--        <el-input-->
      <!--          clearable-->
      <!--          v-model="form.imagePath"-->
      <!--          placeholder="请输入图片存储路径"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="图片链接" prop="imageUrl">-->
      <!--        <el-input-->
      <!--          :rows="4"-->
      <!--          type="textarea"-->
      <!--          v-model="form.imageUrl"-->
      <!--          placeholder="请输入图片链接"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-row>
        <el-col>
          <el-form-item label="备注" prop="remark">
            <el-input
              :rows="4"
              type="textarea"
              v-model="form.remark"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="updateModelValue(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import {ref, reactive, watch, onMounted} from 'vue';
import {EleMessage, toTree} from 'ele-admin-plus/es';
import {useFormData} from '@/utils/use-form-data';
import {addItem, dropDownListItems} from '@/api/cms/nav/item';
import ImageUpload from "@/views/cms/components/ImageUpload/index.vue";
import {handleTree} from "@/utils/linyang";

const emit = defineEmits(['done', 'update:modelValue']);

const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  /** 修改回显的数据 */
  data: Object,

  typeId: Number
});

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  id: void 0,
  name: '',
  parentId: void 0,
  orderNum: void 0,
  status: '',
  imageObjectName: '',
  imagePath: '',
  imageUrl: '',
  redirectUrl: '',
  updateBy: '',
  updateTime: '',
  remark: '',
  typeId: void 0,
});

/** 表单验证规则 */
const rules = reactive({});

/** 保存编辑 */
const save = () => {
  if (!props.typeId) {
    EleMessage.warning("请先在左侧选择一个所属链接类别");
    return;
  }
  form.typeId = props.typeId;
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    addItem(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success(msg);
        updateModelValue(false);
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error(e.message);
      });
  });
};

/** 更新modelValue */
const updateModelValue = (value) => {
  emit('update:modelValue', value);
};


const treeProps = reactive({
  data: [],
  expandOnClickNode: false
});

watch(
  () => props.modelValue,
  (modelValue) => {
    if (modelValue) {
      selectDropDownListItems();
    } else {
      resetFields();
      formRef.value?.clearValidate?.();
    }
  }
);

const selectDropDownListItems = () => {
  dropDownListItems({typeId: props.typeId}).then(res => {
    let tempList = [];
    for (let i = 0; i < res.data.length; i++) {
      tempList.push({id: res.data[i].id, parentId: res.data[i].parentId, label: res.data[i].name});
    }
    treeProps.data = handleTree(tempList, 'id', 'parentId', 'children');
  }).catch((e) => {
    EleMessage.error(e.message);
  });
}
onMounted(() => {
  selectDropDownListItems();
})

</script>
